package com.aode.music.adapter.chat;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.response.News;
import com.aode.music.R;
import com.aode.music.databinding.ItemFriendBinding;

import java.util.ArrayList;

public class ChatAdapter extends RecyclerView.Adapter{
    private Context context;
    private ArrayList<News> newsArrayList;
    private ChatInterface chatInterface;
    public ChatAdapter(Context context,ArrayList<News> newsArrayList,ChatInterface chatInterface){
        this.context = context;
        this.newsArrayList = newsArrayList;
        this.chatInterface = chatInterface;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemFriendBinding friendBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_friend,parent,false);
        return new NewsHolder(friendBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if(holder instanceof NewsHolder){
                NewsHolder newsHolder = (NewsHolder) holder;
                if(!newsArrayList.isEmpty()&&newsArrayList.size()>0){
                    newsHolder.getFriendBinding().setNews(newsArrayList.get(position));
                    newsHolder.getFriendBinding().setChatInterface(chatInterface);
                }
            }
    }

    @Override
    public int getItemCount() {
        return newsArrayList.size();
    }
    class NewsHolder extends RecyclerView.ViewHolder{
        private ItemFriendBinding friendBinding;
        public NewsHolder(ItemFriendBinding friendBinding) {
            super(friendBinding.getRoot());
            this.friendBinding = friendBinding;
        }

        public ItemFriendBinding getFriendBinding() {
            return friendBinding;
        }

        public void setFriendBinding(ItemFriendBinding friendBinding) {
            this.friendBinding = friendBinding;
        }
    }


    public interface ChatInterface{
        void onItemClick(News news);
    }
}
